package lib

import (
	"strconv"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         67,
		Discription: "二进制求和",
		Level:       1,
		Labels: map[string]int{
			"位数操作":  1,
			"双指针滑动": 1,
		},
	})
}

func AddBinary(a string, b string) string {
	i := len(a) - 1
	j := len(b) - 1
	overflow := 0
	str := ""
	for i >= 0 || j >= 0 {
		num1 := 0
		num2 := 0
		if i >= 0 {
			if a[i] == '1' {
				num1 = 1
			}
			i--
		}
		if j >= 0 {
			if b[j] == '1' {
				num2 = 1
			}
			j--
		}
		sum := num1 + num2 + overflow
		if sum >= 2 {
			sum -= 2
			overflow = 1
		} else {
			overflow = 0
		}

		str = strconv.Itoa(sum) + str
	}

	if overflow == 1 {
		str = "1" + str
	}

	return str
}
